import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.util.EmptyStackException;
import java.util.Stack;

public class CardPile {
	protected int x;
	protected int y;
	protected Stack thePile;

	CardPile(int x1, int y1) {
		x = x1;
		y = y1;
		thePile = new Stack();
	}

	public final Card top() {
		return (Card) thePile.peek();
	}

	public final Card pop() {
		try {
			return (Card) thePile.pop();
		} catch (EmptyStackException e) {
			return null;
		}
	}

	public boolean canTake(Card aCard) {
		return false;
	}

	public void addCard(Card aCard) {
		thePile.push(aCard);
	}

	public final boolean isEmpty() {
		return thePile.empty();
	}

	public boolean includes(int tx, int ty) {
		return x <= tx && tx <= x + Card.width && y <= ty
				&& ty <= y + Card.height;
	}

	public void select() {
		if(!isEmpty())
		if (!top().isFaceup())
			top().flip();
	}

	public void display(Component c, Graphics g) {
		g.setColor(Color.blue);
		if (isEmpty()) {
			g.drawRect(x, y, Card.width, Card.height);
		} else {
			if (top() != null)
				try {
					top().draw(c, g, x, y);
				} catch (Exception e) {
					System.out.println(e);
				}
		}
	}

}
